package com.nmu.app.Shrimp.veiw.javagame.render;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import com.nmu.app.Shrimp.veiw.javagame.IVisableObject;
import com.nmu.app.Shrimp.veiw.javagame.dto.Albt;
import com.nmu.app.Shrimp.veiw.javagame.dto.ContextFor2D;
import com.nmu.app.Shrimp.veiw.javagame.util.KeyboardInput;

public class MultiItemMove extends JFrame implements Runnable{
	private volatile boolean running;
	private Thread gameThread;
	private BufferStrategy bufferStrategy;
	private KeyboardInput keyboard;
	private int deltaX=0;
	private int deltaY=0;
	private List<Point> points;
	private int ListRound=0;
	private List<IVisableObject> visableObjects=new ArrayList<IVisableObject>();
	int currentX;
	int currentY;
	
	protected void createAndShowGUI(){
//		Canvas canvas2=new Canvas();
//		canvas2.setSize(30,30);
//		canvas2.setBackground(Color.GREEN);
//		canvas2.setIgnoreRepaint(true);
//		
//		getContentPane().add(canvas2);

		
		Canvas canvas=new Canvas();
		canvas.setSize(640,480);
		canvas.setBackground(Color.BLACK);
		canvas.setIgnoreRepaint(true);
		getContentPane().add(canvas);
		setTitle("simple movement");
		setIgnoreRepaint(true);
		pack();
		keyboard=new KeyboardInput();
		canvas.addKeyListener(keyboard);
		setVisible(true);
		canvas.createBufferStrategy(2);
		bufferStrategy=canvas.getBufferStrategy();
		ContextFor2D ctx=new ContextFor2D();
		ctx.setGravity(1);
		ctx.staticY=460;
		
		IVisableObject a=new Albt(300, 300, 30, 30,ctx);
		
		visableObjects.add(a);
		canvas.requestFocus();
		
		
		
		gameThread=new Thread(this);
		gameThread.start();
	}
	
	
	
	
	
	
	
	@Override
	public void run() {
		running=true;
		while(running){
			gameLoop();
		}
		
	}
	public void gameLoop(){
		processInput();
		updateObject();
		renderFrame();
		sleep(5);
	}
	
	private void updateObject() {
		for(IVisableObject obj:visableObjects){
			obj.update(new Point(currentX,currentY));
		}
	}







	private void renderFrame(){
		do{
			do{
				Graphics g=null;
				try{
					g=bufferStrategy.getDrawGraphics();
					//清除
					g.clearRect(0, 0, getWidth(), getHeight());
					//重绘
					render(g);
					for(IVisableObject iVisableObject:visableObjects){
						iVisableObject.render(g);
					}
				}finally{
					if(null!=g)g.dispose();
				}
			}while(bufferStrategy.contentsRestored());
			bufferStrategy.show();
		}while(bufferStrategy.contentsLost());
	}
	private void render(Graphics g){
		g.setColor(Color.RED);
		g.drawRect(0, 460,500, 10);
//		if(ListRound>0){
//			currentX=deltaX;
//			currentY=points.get(points.size()-ListRound).y;
//			int nextY=currentY;
//			if(ListRound>1)
//			nextY=points.get(points.size()-ListRound+1).y;
//			
//			//knock
//			if(currentX>270&&currentX<350&&(currentY==270||(currentY<270&&nextY>270))
//					&&ListRound<=points.size()/2){
//				g.drawRect(currentX, 270, 30, 30);
//			}else{
//				g.drawRect(currentX, currentY, 30, 30);
//				ListRound--;				
//			}
//		}else{
//			currentX=deltaX;
//			currentY=deltaY;
//			g.drawRect(deltaX, deltaY, 30, 30);	
//		}
		//障碍物
			g.drawRect(300, 300, 50, 10);
	}

	private void processInput(){
		for(IVisableObject obj:visableObjects){
			obj.inputProcess(keyboard);
		}
//		
//		
//		
//		keyboard.poll();
////		if(keyboard.keyDown(KeyEvent.VK_UP)){
////			if(0==ListRound)
////			deltaY=deltaY>1?deltaY-1:deltaY/2;
////			System.out.println("Y-1:"+deltaY);
////		}
////		if(keyboard.keyDown(KeyEvent.VK_DOWN)){
////			if(0==ListRound)
////			deltaY+=1;
////			System.out.println("Y+1:"+deltaY);
////		}
//		if(keyboard.keyDown(KeyEvent.VK_LEFT)){
//			deltaX=deltaX>2?deltaX-2:deltaX/2;
//			System.out.println("X-2:"+deltaX);
//		}
//		if(keyboard.keyDown(KeyEvent.VK_RIGHT)){
//			deltaX+=2;
//			System.out.println("X+2:"+deltaX);
//		}
//		if(keyboard.keyDownOnce(KeyEvent.VK_C)){
//			deltaY=deltaX=0;
//		}
//		if(keyboard.keyDownOnce(KeyEvent.VK_SPACE)){
////			if(0==ListRound){
////				points=getJumpList(deltaY);
////				ListRound=points.size();
////				System.out.println("jump once");
////			}else{
////				System.out.println("can't double jump!");
////			}
//			
//			
//		}
		
	}
	private void sleep(long sleep){
		try{
			Thread.sleep(sleep);
		}catch(InterruptedException e){}
	}
	protected void onWindowClosing(){
		try{
			running=false;
			gameThread.join();
		}catch( InterruptedException e){
			e.printStackTrace();
		}
		System.exit(0);
	}
	
	public List<Point> getJumpList(int currentY){
		//y=-(x^2)/16+256
		List<Point> points=new ArrayList<Point>();
		for(int i=-64;i<=64;i++){
			Point p=new Point(i,currentY-((4096-i*i)/16));
			points.add(p);
		}
		return points;
	}
	
	
	
	public static void main(String[] args) {
		final MultiItemMove app=new MultiItemMove();
		app.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				app.onWindowClosing();
			}
		});
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				app.createAndShowGUI();
			}
		});
	
	}
	
	
	
}
